package com.it.bigdata.parse.imp;

import com.it.bigdata.domian.TieZi;
import com.it.bigdata.parse.Parse;
import com.it.bigdata.schedual.Schedual;
import com.it.bigdata.schedual.imp.BDTBSchedualForQueue;
import com.it.bigdata.schedual.imp.BDTBSchedualForRedis;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class BDTBParsePage implements Parse {
    private HtmlCleaner htmlCleaner=new HtmlCleaner();
    //public Schedual schedual=BDTBSchedualForQueue.getBdtsq();
    public Schedual schedual= BDTBSchedualForRedis.getBdtbSR();
    /**
     * 将解析的页面结果放入队列调度器中
     * @param html
     *
     */
    public void parsePageList(String html) {
        TagNode clean = htmlCleaner.clean(html);
        //解析出页面中需要的url
        try {
            Object[] objects = clean.evaluateXPath("//a[@class='j_th_tit ']/@href");
            for (Object object : objects) {
                schedual.push(object.toString());
            }
        } catch (XPatherException e) {
            e.printStackTrace();
        }
    }
    /**
     * 解析具体页中的数据
     * @param html 具体页的字符形式内容
     * @return  封装的内容对象
     */
    public TieZi parseCurrentTOtiezi(String html){
        TagNode clean = htmlCleaner.clean(html);
        TieZi tieZi=new TieZi();

        try {
            //发帖人
            Object[] author = clean.evaluateXPath("//*[@id=\"j_p_postlist\"]/div[1]/div[1]/ul/li[3]/a/text()");
            if (author!=null&&author.length>0){
                tieZi.setAuthor(author[0].toString());
            }

            //标题
            Object[] title = clean.evaluateXPath("//h3[@class='core_title_txt pull-left text-overflow  ']/text()");
            if (title!=null&&title.length>0){
                tieZi.setTitle(title[0].toString());
            }

            //图片地址
            Object[] images = clean.evaluateXPath("//img[@class='BDE_Image']/@src");
            if (images!=null&&images.length>0){
                for (Object image : images) {
                    tieZi.getImage().add(image.toString());
                }
            }
            return tieZi;
        } catch (XPatherException e) {
            e.printStackTrace();
        }
        return null;
    }
}
